package com.raingo.cc.ledctrl.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.raingo.cc.ledctrl.R;
import com.raingo.cc.ledctrl.hardlib.LedHardControl;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private Button mBtnAll, mBtnLed1, mBtnLed2, mBtnLed3, mBtnLed4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int isLedOpen = LedHardControl.led_open();

        mBtnAll = (Button) findViewById(R.id.btn_all);
        mBtnLed1 = (Button) findViewById(R.id.btn_1);
        mBtnLed2 = (Button) findViewById(R.id.btn_2);
        mBtnLed3 = (Button) findViewById(R.id.btn_3);
        mBtnLed4 = (Button) findViewById(R.id.btn_4);

        mBtnAll.setOnClickListener(this);
        mBtnLed1.setOnClickListener(this);
        mBtnLed2.setOnClickListener(this);
        mBtnLed3.setOnClickListener(this);
        mBtnLed4.setOnClickListener(this);
    }

    boolean ledon = false;
    @Override
    public void onClick(View v) {
        ledon = !ledon;

        if (v == mBtnAll) {
            Log.d(TAG, "onClick: btnall");
            LedHardControl.led_ctrl(0,ledon?1:0);
            LedHardControl.led_ctrl(1,ledon?1:0);
            LedHardControl.led_ctrl(2,ledon?1:0);
            LedHardControl.led_ctrl(3,ledon?1:0);
        } else if (v == mBtnLed1) {
            Log.d(TAG, "onClick: btnled 1");
            LedHardControl.led_ctrl(0,ledon?1:0);
        } else if (v == mBtnLed2) {
            Log.d(TAG, "onClick: btnled 2");
            LedHardControl.led_ctrl(1,ledon?1:0);
        } else if (v == mBtnLed3) {
            Log.d(TAG, "onClick: btnled 3");
            LedHardControl.led_ctrl(2,ledon?1:0);
        } else if (v == mBtnLed4) {
            Log.d(TAG, "onClick: btnled 4");
            LedHardControl.led_ctrl(3,ledon?1:0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LedHardControl.led_close();
    }
}
